import time
import os

from werkzeug.utils import secure_filename


def save_image(file):

    file.filename = "%s.%s" % (int(time.time() * 1000), file.filename.split(".")[-1])
    save_path = os.path.join(os.getcwd(), "app/images", secure_filename(file.filename))
    file.save(save_path)

    return file.filename


def delete_image(file_name):

    try:
        os.remove(os.path.join(os.getcwd(), "app/images", file_name))
    except Exception as e:
        raise e
    return 0


def get_link(file_name):

    return "http://" + os.getenv("DB_HOST", "127.0.0.1") + ":8001" + "/images/" + file_name
